//
//  MoreDJController.swift
//  DiZhenYuJing
//
//  Created by zhibao on 2024/11/2.
//

import UIKit
import SnapKit
@objc class MoreDJController:SuperController,UITableViewDelegate,UITableViewDataSource,DataChangeDelegate{
    fileprivate var dataArray = [[String:Any]]()
    fileprivate var showTableView:MYTableView!
    var year = "2024"
    var pageNum = 1
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.gk_navTitle = LocalizedString("等级排名")
        
        let calendar = Calendar.current
        let components = calendar.component(.year, from: Date())
        let currentYear = components
        year = "\(currentYear)"
        
        self.initTableView()
        self.getListData()
        self.view.backgroundColor = MYColor.color(withHexString: "f5f5f8")
    }
    
    func initTableView() {
        showTableView = MYTableView(frame: CGRectMake(0, navigationFullHeight(), SCREEN_WIDTH, SCREEN_HEIGHT - navigationFullHeight()), style: .plain)
        showTableView.backgroundColor = MYColor.color(withHexString: "f5f5f8")
        showTableView.delegate = self
        showTableView.dataSource = self
        showTableView.dataChangeDelegate = self
        showTableView.separatorStyle = .none
        showTableView.register(TongjiCell.self, forCellReuseIdentifier: "TongjiCell")
        showTableView.register(HomeCell.self, forCellReuseIdentifier: "HomeCell")
        self.view.addSubview(showTableView)
        showTableView.noDataType = .TableViewNoDataType_FILE
    }
}

extension MoreDJController {
    func getListData(){
        var paras = [AnyHashable : Any]()
        paras["source"] = 0
        paras["timeZone"] = "8"
        paras["pageSize"] = 20
        paras["zjMin"] = 1
        paras["pageNum"] = pageNum
        paras["dateEnd"] = Helper.getCurrentTimes("yyyy-MM-dd")
        paras["position"] = "\(MYGlobal.share().locationModel.lat),\(MYGlobal.share().locationModel.lon)"
        paras["zjMin"] = 10
        paras["dateStart"] = "\(year)-01-01"
        
        WYHttpHelper.sharedInstance().upload(withUrl: "alarm/getTop", paras: paras) { task, resposeMdoe in
            DLog(message: "getTop.data==\(String(describing: resposeMdoe.data))")
            let data = resposeMdoe.data as! [String:Any]
            let result = data["result"] as! [String:Any]
            let earthquakes = result["earthquakes"] as! [String:Any]
            let content = earthquakes["content"] as! [[String:Any]]
            self.dataArray = self.dataArray + content
            self.showTableView.reloadData()
            if content.count == 0 {
                self.showTableView.mj_footer?.endRefreshingWithNoMoreData()
            }
        } failure: { task, error in
            
        }

        
    }
    func refreshData(withTabelViewTag tableViewTag: Int) {
        pageNum = 1
        self.dataArray.removeAll()
        self.getListData()
    }
    
    func loadMoreData(withTabelViewTag tableViewTag: Int) {
        pageNum += 1
        self.getListData()
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let dic = dataArray[indexPath.row]
        if indexPath.row < 3 {
            var dic1 = [String:Any]()
            dic1["LOCATION_C"] = dic["location"]
            dic1["O_TIME"] = dic["time"]
            dic1["EPI_LON"] = dic["lon"]
            dic1["EPI_LAT"] = dic["lat"]
            dic1["EPI_DEPTH"] = dic["depth"]
            dic1["M"] = dic["mag"]
            let cell = tableView.dequeueReusableCell(withIdentifier: "HomeCell") as! HomeCell
            cell.updata(dic: dic1, index: indexPath.row)
            return cell
        }else{
            let cell = tableView.dequeueReusableCell(withIdentifier: "TongjiCell") as! TongjiCell
            cell.updata(dic: dic)
            return cell
        }
        
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let dic = dataArray[indexPath.row]
        let controller = MapDetailController()
        var dic1 = [String:Any]()
        dic1["LOCATION_C"] = dic["location"]
        dic1["O_TIME"] = dic["time"]
        dic1["EPI_LON"] = dic["lon"]
        dic1["EPI_LAT"] = dic["lat"]
        dic1["EPI_DEPTH"] = dic["depth"]
        dic1["M"] = dic["mag"]
        controller.dic = dic1
        self.navigationController?.pushViewController(controller, animated: true)
        
    }
    
    @objc func toMore(){
        let controller = MoreDJController()
        self.navigationController?.pushViewController(controller, animated: true)
    }
    
}
